package com.wbs.service;

import com.google.common.base.Preconditions;
import com.wbs.beans.PageQuery;
import com.wbs.beans.PageResult;
import com.wbs.dao.SysUserMapper;
import com.wbs.exception.ParamException;
import com.wbs.model.SysUser;
import com.wbs.param.UserParam;
import com.wbs.util.BeanValidator;
import com.wbs.util.MD5Util;
import com.wbs.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysUserService {

    @Autowired
    SysUserMapper sysUserMapper;

    public void save(UserParam param) {
        BeanValidator.check(param);
        int id = param.getId();
        if (checkTelephoneExist(param.getTelephone(), param.getId())) {
            throw new ParamException("电话被占用");
        }
        if (checkEmailExist(param.getMail(), param.getId())) {
            throw new ParamException("邮箱被占用");
        }

        String password = PasswordUtil.randomPassword();
        password = "123456";
        String encryptedPassword = MD5Util.encrypt(password);  // 进行MD5加密

        SysUser user = SysUser.builder().username(param.getUsername()).telephone(param.getTelephone()).mail(param.getMail())
                .password(encryptedPassword).deptId(param.getDeptId()).status(param.getStatus()).remark(param.getRemark()).build();

        user.setOperateIp("127.0.0.1");  // TODO:
        user.setOperateTime(new Date());  // TODO:
        user.setOperator("system"); // TODO:

        // TODO :sendemail
        sysUserMapper.insertSelective(user);
    }

    public void update(UserParam param) {
        BeanValidator.check(param);
        if (checkTelephoneExist(param.getTelephone(), param.getId())) {
            throw new ParamException("电话被占用");
        }
        if (checkEmailExist(param.getMail(), param.getId())) {
            throw new ParamException("邮箱被占用");
        }
        SysUser before = sysUserMapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before, "待更新的用户不能为空");
        SysUser after = SysUser.builder().id(param.getId()).username(param.getUsername()).telephone(param.getTelephone()).mail(param.getMail())
                .password(before.getPassword()).deptId(param.getDeptId()).status(param.getStatus()).remark(param.getRemark()).build();
        sysUserMapper.updateByPrimaryKeySelective(after);  // 有值的时候才更新


    }

    // 检查邮箱是否存在
    public boolean checkEmailExist(String mail, Integer userId) {

        return sysUserMapper.countByMail(mail, userId) > 0;
    }

    // 检查电话是否存在
    public boolean checkTelephoneExist(String telephone, Integer userId) {

        return sysUserMapper.countByTelephone(telephone, userId) > 0;

    }

    // 查找登录的用户信息，keyword是按照邮箱或者电话号码来查找，所以登录的时候可以根据邮箱或者电话号码登录
    public SysUser findByKeyword(String keyword) {
        return sysUserMapper.findByKeyword(keyword);
    }

    public PageResult<SysUser> getPageByDeptId(int deptId, PageQuery page) {
        BeanValidator.check(page);
        int count = sysUserMapper.countByDeptId(deptId);  // 根据部门号查询总用户数
        if (count > 0) {
            List<SysUser> list = sysUserMapper.getPageByDeptId(deptId, page);
            return PageResult.<SysUser>builder().total(count).data(list).build();
        }
        return PageResult.<SysUser>builder().build();
    }

}
